package com.btpn.custody.security.action;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import com.btpn.custody.common.Constant;
import com.btpn.custody.dao.IGroupRole;
import com.btpn.custody.entity.GroupRole;
import com.btpn.custody.service.IGroupRoleService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

@ParentPackage(value="custody")
@Results({
	@Result(name="input", location="t_group_role")
})
@Validations(
	requiredStrings = {
		@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "groupName", message = "Group name must be filled")
    }
)
public class GroupRoleAction extends ActionSupport implements ServletRequestAware, Preparable {

	private static final long serialVersionUID = -906717310389015173L;

	@Autowired
	private IGroupRole groupRoleDaoHibernate;
	
	@Autowired
	private IGroupRoleService groupRoleService;
	
	private HttpServletRequest request;
	
	private Long groupRoleId;
	private String groupName;
	private String description;
	private Boolean role01;
	private Boolean role02;
	private Boolean role03;
	private Boolean role04;
	private Boolean role05;
	private Boolean role06;
	private Boolean role07;
	private Boolean role08;
	private Boolean role09;
	private Boolean role10;
	private Boolean role11;
	private Boolean role12;
	private Boolean role13;
	private Boolean role14;
	private Boolean role15;	
	
	private List<GroupRole> groupRoles;
	
	@SkipValidation
	@Action(value = "/listGroupRole", results = { @Result(name="success", location="t_group_role") })
	public String execute() throws Exception {
				
		groupRoleId=null;
		setGroupName(null);
		setDescription(null);
		
		return SUCCESS;
	}
	
	@Action(value = "/addOrUpdateGroupRole", results = {	
			@Result(name="success", type="redirectAction", params = {"actionName", "listGroupRole"} )
		}
	)
	public String addOrUpdateUser() throws Exception {
		if (groupRoleId==null) { 			// create
			// validate group name
			GroupRole theGroupRole = groupRoleDaoHibernate.getByGroupName(groupName);
			if (theGroupRole != null) {
				addFieldError("groupName", "Group name already exist");
				return INPUT;
			}
		}
		
		GroupRole groupRole = null;		
		if (groupRoleId == null) {
			groupRole = new GroupRole();									// create
			groupRole.setCreatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
		} else {
			groupRole = groupRoleDaoHibernate.get(groupRoleId);				// update
			groupRole.setLastUpdatedDate(new Date());
			groupRole.setLastUpdatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
		}
		
		groupRole.setGroupName(groupName);
		groupRole.setGroupDesc(description);
		
		Set<String> roles = new HashSet<String>(0);
		if (role01) roles.add(Constant.ROLE_REGION);
		if (role02) roles.add(Constant.ROLE_AREA);
		if (role03) roles.add(Constant.ROLE_BRANCH);
		if (role04) roles.add(Constant.ROLE_ADMIN);
		if (role05) roles.add(Constant.ROLE_PARAMETER);
		if (role06) roles.add(Constant.ROLE_MANUAL);
		if (role07) roles.add(Constant.ROLE_RECEIVING);
		if (role08) roles.add(Constant.ROLE_COMPLETING);
		if (role09) roles.add(Constant.ROLE_CHECKLIST);
		if (role10) roles.add(Constant.ROLE_APPROVAL);
		if (role11) roles.add(Constant.ROLE_REPORT1);
		if (role12) roles.add(Constant.ROLE_REPORT2);
		if (role13) roles.add(Constant.ROLE_REPORT3);
		if (role14) roles.add(Constant.ROLE_REPORT4);
		if (role15) roles.add(Constant.ROLE_REPORT5);		
		groupRole.setRoles(roles);
		
		groupRoleDaoHibernate.createOrUpdate(groupRole);
		
		return SUCCESS;
	}
	
	@SkipValidation
	@Action(value = "/editGroupRole", results = { 
			@Result(name="success", location="t_group_role"),			
			@Result(name="error", type="redirectAction", params = {"actionName", "listGroupRole"})
	})
	public String editGropRole() throws Exception {
				
		String groupId = getRequest().getParameter("id");
		if (groupId != null) {
			GroupRole editedGroup = groupRoleDaoHibernate.get(Long.parseLong(groupId));
			groupName = editedGroup.getGroupName();
			description = editedGroup.getGroupDesc();			
			groupRoleId = editedGroup.getId();
			
			Set<String> roles = editedGroup.getRoles();
			role01 = (roles.contains(Constant.ROLE_REGION));
			role02 = (roles.contains(Constant.ROLE_AREA));
			role03 = (roles.contains(Constant.ROLE_BRANCH));
			role04 = (roles.contains(Constant.ROLE_ADMIN));
			role05 = (roles.contains(Constant.ROLE_PARAMETER));
			role06 = (roles.contains(Constant.ROLE_MANUAL));
			role07 = (roles.contains(Constant.ROLE_RECEIVING));
			role08 = (roles.contains(Constant.ROLE_COMPLETING));
			role09 = (roles.contains(Constant.ROLE_CHECKLIST));
			role10 = (roles.contains(Constant.ROLE_APPROVAL));
			role11 = (roles.contains(Constant.ROLE_REPORT1));
			role12 = (roles.contains(Constant.ROLE_REPORT2));
			role13 = (roles.contains(Constant.ROLE_REPORT3));
			role14 = (roles.contains(Constant.ROLE_REPORT4));
			role15 = (roles.contains(Constant.ROLE_REPORT5));
			
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	@SkipValidation
	@Action(value = "/removeGroupRole", results = { 
			@Result(name="success", type="redirectAction", params = {"actionName", "listGroupRole"} )
	})
	public String removeGroupRole() throws Exception {
		groupRoleService.deleteGroupRole(Long.parseLong(getRequest().getParameter("id")));	
		return SUCCESS;
	}
	
	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Long getGroupRoleId() {
		return groupRoleId;
	}

	public void setGroupRoleId(Long groupRoleId) {
		this.groupRoleId = groupRoleId;
	}

	public List<GroupRole> getGroupRoles() {
		return groupRoles;
	}

	public void setGroupRoles(List<GroupRole> groupRoles) {
		this.groupRoles = groupRoles;
	}

	public Boolean getRole01() {
		return role01;
	}

	public void setRole01(Boolean role01) {
		this.role01 = role01;
	}

	public Boolean getRole02() {
		return role02;
	}

	public void setRole02(Boolean role02) {
		this.role02 = role02;
	}

	public Boolean getRole03() {
		return role03;
	}

	public void setRole03(Boolean role03) {
		this.role03 = role03;
	}

	public Boolean getRole04() {
		return role04;
	}

	public void setRole04(Boolean role04) {
		this.role04 = role04;
	}

	public Boolean getRole05() {
		return role05;
	}

	public void setRole05(Boolean role05) {
		this.role05 = role05;
	}

	public Boolean getRole06() {
		return role06;
	}

	public void setRole06(Boolean role06) {
		this.role06 = role06;
	}

	public Boolean getRole07() {
		return role07;
	}

	public void setRole07(Boolean role07) {
		this.role07 = role07;
	}

	public Boolean getRole08() {
		return role08;
	}

	public void setRole08(Boolean role08) {
		this.role08 = role08;
	}

	public Boolean getRole09() {
		return role09;
	}

	public void setRole09(Boolean role09) {
		this.role09 = role09;
	}

	public Boolean getRole10() {
		return role10;
	}

	public void setRole10(Boolean role10) {
		this.role10 = role10;
	}

	public Boolean getRole11() {
		return role11;
	}

	public void setRole11(Boolean role11) {
		this.role11 = role11;
	}

	public Boolean getRole12() {
		return role12;
	}

	public void setRole12(Boolean role12) {
		this.role12 = role12;
	}

	public Boolean getRole13() {
		return role13;
	}

	public void setRole13(Boolean role13) {
		this.role13 = role13;
	}

	public Boolean getRole14() {
		return role14;
	}

	public void setRole14(Boolean role14) {
		this.role14 = role14;
	}

	public Boolean getRole15() {
		return role15;
	}

	public void setRole15(Boolean role15) {
		this.role15 = role15;
	}

	@Override
	public void prepare() throws Exception {
		groupRoles = groupRoleDaoHibernate.findAll();
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletRequest getRequest() {
		return request;
	}
}
